﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class HoaDonTraPhongDAL:StandardConnectDB,IHoaDonTraPhong
    {
        public List<DTO.HoaDonTraPhongDTO> GetlistHoaDon()
        {
            List<HoaDonTraPhongDTO> listHoaDonTraPhong = new List<HoaDonTraPhongDTO>();
            DataTable dt = DTExecuteSelect("Select * from HoaDonTraPhong");
            foreach (DataRow dr in dt.Rows)
            {
                listHoaDonTraPhong.Add(ConvertObjectDataRowToHoaDonTraPhongDTO(dr));
            }
            return listHoaDonTraPhong;
            
            
        }


        public List<HoaDonTraPhongDTO> GetlistHoaDonTraPhong()
        {
            List<HoaDonTraPhongDTO> listHoaDonTraPhong = new List<HoaDonTraPhongDTO>();
            DataTable dt = DTExecuteSelect("Select * from HoaDonTraPhong");
            foreach (DataRow dr in dt.Rows)
            {
                listHoaDonTraPhong.Add(ConvertObjectDataRowToHoaDonTraPhongDTO(dr));
            }
            return listHoaDonTraPhong;
        }



        public DTO.HoaDonTraPhongDTO GetHoaDonTraPhongByID(Int64 MaSoHD)
        {
            HoaDonTraPhongDTO hoadon = new HoaDonTraPhongDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaSoHD", MaSoHD);
            DataTable dt = DTExecuteSelect("SP_HoaDonTraPhong_GetByID", paramlist);
            hoadon = ConvertObjectDataRowToHoaDonTraPhongDTO(dt.Rows[0]);
            return hoadon;
            
        }

        public HoaDonTraPhongDTO GetHoaDonTraPhongByMaDangKy(PhieuDangKyPhongDTO phieudk)
        {
            HoaDonTraPhongDTO hoadon = new HoaDonTraPhongDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaPhieuDK", phieudk.MaPhieu);
            paramlist.Add("@MaPhong", phieudk.MaPhong.MaPhong);
            DataTable dt = DTExecuteSelect("SP_HoaDonTraPhong_GetByPhieuDangKy", paramlist);
            hoadon = ConvertObjectDataRowToHoaDonTraPhongDTO(dt.Rows[0]);
            return hoadon;
        }



        public List<DTO.HoaDonTraPhongDTO> GetAllHoaDonTraPhongByMaND(string MaND)
        {
            List<HoaDonTraPhongDTO> listHoaDonTraPhong = new List<HoaDonTraPhongDTO>();
            DataTable dt = DTExecuteSelect("SP_HoaDonTraPhong_GetByMaND");
            foreach (DataRow dr in dt.Rows)
            {
                listHoaDonTraPhong.Add(ConvertObjectDataRowToHoaDonTraPhongDTO(dr));
            }
            return listHoaDonTraPhong;
            
        }

        public List<DTO.HoaDonTraPhongDTO> GetAllHoaDonTraPhongByMaPhieuDat(string MaPhieuDat)
        {
            List<HoaDonTraPhongDTO> listHoaDonTraPhong = new List<HoaDonTraPhongDTO>();
            DataTable dt = DTExecuteSelect("HoaDonTraPhong_GetByMaPhieuDat");
            foreach (DataRow dr in dt.Rows)
            {
                listHoaDonTraPhong.Add(ConvertObjectDataRowToHoaDonTraPhongDTO(dr));
            }
            return listHoaDonTraPhong;
            
        }

        public List<DTO.HoaDonTraPhongDTO> GetAllHoaDonTraPhongByMaPhong(string MaPhong)
        {
            throw new NotImplementedException();
        }

        public List<DTO.HoaDonTraPhongDTO> GetAllHoaDonTraPhongByMaKH(string MaKH)
        {
            throw new NotImplementedException();
        }

        public bool InsertHoaDonTraPhong(DTO.HoaDonTraPhongDTO hoadon)
        {

            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaSoHD", hoadon.MaSoHD);
            paramlist.Add("@MaND", hoadon.MaND.Username);
            paramlist.Add("@MaPhieuDat", hoadon.MaPhieuDat.MaPhieu);
            paramlist.Add("@MaPhong", hoadon.MaPhong.MaPhong);
            paramlist.Add("@SoTienPhaiTra", hoadon.SoTienPhaiTra);
            paramlist.Add("@NgayTra", hoadon.NgayTra);
            paramlist.Add("@SoTienTra", hoadon.SoTienTra);

            if (hoadon.MaKHGhiHD != null)
                paramlist.Add("@MaKhGhiHD", hoadon.MaKHGhiHD.MaKH);
            else
                paramlist.Add("@MaKhGhiHD", "");

            paramlist.Add("@SoHD", hoadon.SoHD);

            paramlist.Add("@NgayGhiHD", hoadon.NgayGhiHD);
            
            paramlist.Add("@GhiChu", hoadon.GhiChu);
            return ExecuteUpdate("SP_HoaDonTraPhong_Insert", paramlist);
            
        }

        public bool UpdateHoaDonTraPhong(DTO.HoaDonTraPhongDTO hoadon)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaSoHD", hoadon.MaSoHD);
            paramlist.Add("@MaND", hoadon.MaND);
            paramlist.Add("@MaPhieuDat", hoadon.MaPhieuDat);
            paramlist.Add("@MaPhong", hoadon.MaPhong);
            paramlist.Add("@SoTienPhaiTra", hoadon.SoTienPhaiTra);
            paramlist.Add("@NgayTra", hoadon.NgayTra);
            paramlist.Add("@SoTienTra", hoadon.SoTienTra);

            paramlist.Add("@MaKhGhiHD", hoadon.MaKHGhiHD);
            paramlist.Add("@SoHD", hoadon.SoHD);

            if (hoadon.MaKHGhiHD != null)
                paramlist.Add("@MaKhGhiHD", hoadon.MaKHGhiHD);
            else
                paramlist.Add("@MaKhGhiHD", null);

            paramlist.Add("@GhiChu", hoadon.GhiChu);

            return ExecuteUpdate("SP_HoaDonTraPhong_Update", paramlist);
            
            
        }

        public bool DeleteHoaDonTraPhong(DTO.HoaDonTraPhongDTO hoadon)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaSoHD", hoadon.MaSoHD);
            return ExecuteUpdate("SP_HoaDonTraPhong_Delete", paramlist);
            
        }

        public DTO.HoaDonTraPhongDTO ConvertObjectDataRowToHoaDonTraPhongDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                HoaDonTraPhongDTO hoadon = new HoaDonTraPhongDTO();
                hoadon.MaSoHD = Int64.Parse(dr["MaSoHD"].ToString());
             
                if (dr["MaND"].ToString() != "")
                {
                    INguoiDung inguoidung = new NguoiDungDAL();
                    hoadon.MaND = inguoidung.GetNguoiDungByID(dr["MaND"].ToString());
                }

                if (dr["MaPhieuDK"].ToString() != "")
               {
                   IPhieuDangKy iphieudk = new PhieuDangKyDAL();
                   hoadon.MaPhieuDat = iphieudk.GetPhieuDangKyPhongByMaPhieu(long.Parse(dr["MaPhieuDK"].ToString()));

               }


                if (dr["MaPhong"].ToString() != "")
                {
                    IPhong iPhong = new PhongDAL();
                    hoadon.MaPhong = iPhong.GetPhongByID(dr["MaPhong"].ToString());
                }

                hoadon.SoTienPhaiTra = double.Parse(dr["SoTienPhaiTra"].ToString());
                hoadon.NgayTra = DateTime.Parse(dr["NgayTra"].ToString());
                hoadon.SoTienTra = double.Parse(dr["SoTienTra"].ToString());
                if (dr["MaKhGhiHD"].ToString() != "")
                {
                    IKhachHang ikhachhang = new KhachHangDAL();
                    hoadon.MaKHGhiHD=ikhachhang.GetKhachHangByID(dr["MaKhGhiHD"].ToString());
                }
                
                hoadon.SoHD = dr["SoHD"].ToString();

                if (dr["NgayGhiHD"].ToString() != "")
                    hoadon.NgayGhiHD = DateTime.Parse(dr["NgayGhiHD"].ToString());


                hoadon.GhiChu = dr["GhiChu"].ToString();

                return hoadon;
            }
            return null;
           
        }




        public long GetMaxMaHoaDon()
        {
            DataTable dt = this.DTExecuteSelect("SELECT MAX(MaSoHD) AS MaxMaPhieu FROM dbo.HoaDonTraPhong");
            string a = dt.Rows[0]["MaxMaPhieu"].ToString();
            if (a != "")
                return int.Parse(a) + 1;
            return 1;
        }





       
    }
}
